const Router = require('koa-router');
const router = new Router();
const tool = require('../../tools/tool')
const {db, QueryTypes} = require("../../models/db");

// 查询终端
router.post('/deliver/getContactByAreaId', async ctx => {
    let tenantId = ctx.user.tenantId;
    let searchTxt = ctx.request.body.searchTxt;
    let areaId = ctx.request.body.areaId;
    let tids = await tool.relCompany(tenantId);
    let datasql = `select * from v_contact where tenantId in(${tids.join(',')}) and isDel=0 ` // db.table('v_contact').where().in('tenantId', tids).eq('isDel', 0);
    if (searchTxt) {
        datasql += ` and name like '%${searchTxt}%' `
    }
    if (areaId) {
        datasql += ` and areaId=${areaId} `
    }
    datasql += ' order by tenantId,address '
    let list =  await db.query(datasql, {type: QueryTypes.SELECT})
    return ctx.suc('', list);
})

module.exports = router;
